<template>
  <div>
    <el-dialog title="新增角色" :visible="dialogVisible" @close="close">
      <el-form :model="formData" :rules="rules" ref="form">
        <el-form-item label="角色名称" prop="namezn" label-width="120">
          <el-input v-model="formData.namezn"></el-input>
        </el-form-item>
        <el-form-item label="英文名称" prop="name" label-width="120">
          <el-input v-model="formData.name"></el-input>
        </el-form-item>
        <el-form-item label="角色分类" prop="roletype">
          <el-select v-model="formData.roletype" placeholder="请选择分类">
            <el-option
              :label="item.name"
              :value="item.id"
              v-for="(item, index) in roletypes"
              :key="index"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="备注信息">
          <el-input type="textarea" v-model="formData.mark"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer">
        <el-button @click="close">取 消</el-button>
        <el-button type="primary" @click="save">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { checkRole } from "@/api/role";
import { addRole } from "@/api/role";
export default {
  props: {
    dialogVisible: {
      type: Boolean,
      default: true
    }
  },
  data() {
    return {
      //name英文名称，namezn, 中文名称 roletype 1系统管理员 0，单位管理员，mark
      formData: {
        name: "",
        namezn: "",
        mark: "",
        roletype: 1,
        perms: ""
      },
      roletypes: [
        { name: "公务主管部门", id: 1 },
        { name: "用车单位", id: 0 }
      ],
      rules: {
        name: [{ required: true, message: "请输入", trigger: "blur" }],
        namezn: [{ required: true, message: "请输入", trigger: "blur" }],
        roletype: [{ required: true, message: "请输入", trigger: "blur" }]
      }
    };
  },
  methods: {
    close() {
      this.$emit("update:dialogVisible", false);
    },
    save() {
      this.$refs["form"].validate(async valid => {
        if (valid) {
          let d = await checkRole({
            name: this.formData.name
          });
          if (!d) {
            this.$message({
              message: "英文名重复",
              type: "error"
            });
            return;
          }
          let r = await addRole(this.formData);
          if (r && r.error == 0) {
            this.$refs['form'].resetFields()
            this.formData.mark=""
            this.$emit("finish", r);
            this.close();
          } else {
            this.$message({
              message: "出错了",
              type: "error"
            });
          }
        } else {
          return false;
        }
      });
    }
  }
};
</script>
<style lang="scss" scoped>
.el-form-item {
  display: flex;
}
.el-form-item__content {
  flex: 1;
}
.el-select {
  flex: 1;
}
</style>